import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/HomeView.vue'),
    meta: {
      title: 'SHARED-AI - 让每个人都能使用AI'
    }
  },
  {
    path: '/tool-detail/:id',
    name: 'ToolDetail',
    component: () => import('../views/ToolDetailView.vue'),
    meta: {
      title: 'SHARED-AI - 产品详情'
    }
  },
  {
    path: '/privacy-policy',
    name: 'PrivacyPolicy',
    component: () => import('../views/PrivacyPolicy.vue'),
    meta: {
      title: 'SHARED-AI - 隐私政策'
    }
  },
  {
    path: '/user-agreement',
    name: 'UserAgreement',
    component: () => import('../views/UserAgreement.vue'),
    meta: {
      title: 'SHARED-AI - 用户协议'
    }
  },
  {
    path: '/alliance',
    name: 'Alliance',
    component: () => import('../views/AllianceView.vue'),
    meta: {
      title: 'SHARED-AI - 推广者联盟'
    }
  },
  {
    path: '/my-service',
    name: 'MyService',
    component: () => import('../views/MyServiceView.vue'),
    meta: {
      title: 'SHARED-AI - 我的服务'
    }
  },
  {
    path: '/promotion',
    name: 'Promotion',
    component: () => import('../views/PromotionView.vue'),
    meta: {
      title: 'SHARED-AI - 推广'
    }
  },
  {
    path: '/supply',
    name: 'Supply',
    component: () => import('../views/SupplyView.vue'),
    meta: {
      title: 'SHARED-AI - 供给合作'
    }
  },
  {
    path: '/personal-center',
    name: 'PersonalCenter',
    component: () => import('../views/PersonalCenterView.vue'),
    meta: {
      title: 'SHARED-AI - 个人中心'
    }
  },
  {
    path: '/recruit',
    name: 'Recruit',
    component: () => import('../views/RecruitView.vue'),
    meta: {
      title: 'SHARED-AI - 人才招聘'
    }
  },
  {
    path: '/studio',
    name: 'Studio',
    component: () => import('../views/StudioView.vue'),
    meta: {
      title: 'SHARED-AI - 工作室简介'
    }
  },
  {
    path: '/my-benefits',
    name: 'MyBenefits',
    component: () => import('../views/MyBenefitsView.vue'),
    meta: {
      title: 'SHARED-AI - 我的权益'
    }
  },
  {
    path: '/my-promotion',
    name: 'MyPromotion',
    component: () => import('../views/MyPromotionView.vue'),
    meta: {
      title: 'SHARED-AI - 我的推广'
    }
  },
  {
    path: '/my-orders',
    name: 'MyOrders',
    component: () => import('../views/MyOrdersView.vue'),
    meta: {
      title: 'SHARED-AI - 我的订单'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/LoginView.vue'),
    meta: {
      title: 'SHARED-AI - 登录/注册'
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || 'SHARED-AI'
  
  // 不需要登录权限的页面
  const publicPages = [
    '/', 
    '/login', 
    '/alliance', 
    '/supply', 
    '/privacy-policy', 
    '/user-agreement', 
    '/studio', 
    '/recruit'
  ]
  
  // 检查当前页面是否需要登录
  const authRequired = !publicPages.includes(to.path)
  
  // 如果需要登录但未登录，则跳转到登录页
  if (authRequired && !store.getters.isAuthenticated) {
    next({
      path: '/login',
      query: { redirect: to.fullPath } // 将要访问的页面作为参数传递
    })
  } else {
    next()
  }
})

export default router
